package particles;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class ParticlePanel extends JPanel implements ActionListener, MouseListener,  MouseMotionListener, MouseWheelListener{
	Set<Particle> particles;
	Timer t;
	int spawnX;
	int spawnY;
	double xDir = 0.0;
	double yDir = 0.0;
	boolean on;

	public ParticlePanel(JFrame parent){
		particles = new HashSet<Particle>();
		this.setBackground(Color.BLACK);
		t = new Timer(10, this);
		this.setSize(parent.getSize());
		spawnX = this.getWidth()/2;
		spawnY = this.getHeight()/2;
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.addMouseWheelListener(this);
		t.start();
	}

	@Override
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		super.paint(g);
		g2d.setColor(Color.CYAN);
		for(Particle p: particles){

			//g2d.fill(p.getShape());
			g2d.drawImage(p.getImage(), (int)p.getx(), (int)p.gety(), null);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		List<Particle> toRemove = new LinkedList<Particle>();
		for(Particle p: particles){
			if(!p.isInBounds(this.getWidth(), this.getHeight())){
				toRemove.add(p);
			}
		}
		for(Particle p: toRemove){
			particles.remove(p);
			t.removeActionListener(p);
		}

		//makes new particles
		if(on){
			for(int i = 0; i < 10; ++i){
				Particle n = new Particle(spawnX, spawnY, xDir, yDir);
				particles.add(n);
				t.addActionListener(n);
			}
		}
		this.repaint();
	}

	public static void main(String[] args){
		JFrame frame = new JFrame("Particles");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1024, 720);
		ParticlePanel pp = new ParticlePanel(frame);
		
		frame.add(pp);
		frame.setVisible(true);

	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		spawnX = arg0.getX();
		spawnY = arg0.getY();
		on = true;
		//particles = new HashSet<Particle>();

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		on = false;
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		double delX = arg0.getX() - spawnX;
		double delY = arg0.getY() - spawnY;
		//double hyp = Math.hypot(delX, delY);
		xDir = delX;
		yDir = delY;
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {
		Particle.GRAVITY+= .002 * arg0.getWheelRotation();

	}
}
